package com.alan.wifishare.view.impl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.model.business.ScanedWifiInfo;
import com.alan.wifishare.presenter.SHAWifiScanPS;
import com.alan.wifishare.utils.WifiUtil;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVSHAWifiScan;
import com.alan.wifishare.view.ui.SHAScanWifiAdapter;

import java.util.List;

/**
 * @className: SHAWifiScanActivity
 * @describtion: SHAWifiScanActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/27 18:10
 */
public class SHAWifiScanActivity extends BaseActivity implements IVSHAWifiScan, View.OnClickListener {

    private SHAWifiScanPS shaWifiScanPS;
    private Button shaWifiScanBackBtn;
    private Button shaWifiScanTitleRefreshBtn;
    private ListView shaWifiScanListview;
    private SHAScanWifiAdapter shaScanWifiAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sha_wifi_scan_layout);
        shaWifiScanPS = new SHAWifiScanPS(this);
        initView();
        shaWifiScanPS.scanWifi();
    }

    @Override
    protected void initView() {
        shaWifiScanBackBtn = (Button)findViewById(R.id.shaWifiScanBackBtn);
        shaWifiScanTitleRefreshBtn = (Button)findViewById(R.id.shaWifiScanTitleRefreshBtn);
        shaWifiScanBackBtn.setOnClickListener(this);
        shaWifiScanTitleRefreshBtn.setOnClickListener(this);
        shaWifiScanListview = (ListView)findViewById(R.id.shaWifiScanListview);
        shaScanWifiAdapter = new SHAScanWifiAdapter(this);
        shaWifiScanListview.setAdapter(shaScanWifiAdapter);
        shaWifiScanListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ScanedWifiInfo scanedWifiInfo = shaScanWifiAdapter.getItemList().get(i);
                if(scanedWifiInfo.isShared()){
                    toastMsg(scanedWifiInfo.getSSID() + "已被分享，请选择其他的wifi！");
                }else {
                    ActivityJumpUtil.jump2SHAWifiSubmitActToAddWifi(SHAWifiScanActivity.this, scanedWifiInfo.getSSID(), scanedWifiInfo.getMac(), GlobalField.userLocation.getAreaId(),
                            GlobalField.userLocation.getLongitude(), GlobalField.userLocation.getLatitude(), GlobalField.userLocation.getAddressStr());
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        SHAWifiScanActivity.this.finish();
    }

    @Override
    public void showScanedWifiList(List<ScanedWifiInfo> scanedWifiInfoList) {
        shaScanWifiAdapter.setItemList(scanedWifiInfoList);
    }

    @Override
    public void showProgress() {
        progressDialogShow();
    }

    @Override
    public void hideProgress() {
        progressDialogHiden();
    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shaWifiScanBackBtn:
                this.finish();
                break;
            case R.id.shaWifiScanTitleRefreshBtn:
                    shaWifiScanPS.scanWifi();
                break;
            default:
                break;
        }
    }
}
